<?php

namespace app\system\controller;


class Ad extends Base
{
   /**
    * @Notes 广告列表
    */
   public function index() {
       if(request()->isPost()) {
         $pageSize = input('limit/d');
         $cate = input('cate',0);
         $title = input('title','');
         $where = [];
         if($cate) $where['pid'] = $cate;
         if($title) $where['title'] = ['like','%'.$title.'%'];

         $ad = model('ad')->where($where)->paginate($pageSize)->toArray();
         foreach ($ad['data'] as &$v) {
             $v['pid'] = model('ad_class')->where(['id' => $v['pid']])->value('name');
             $v['thumb'] = '<img onclick="previewImg(this)" src="'.$v['thumb'].'" style="height:300px" />'; 
         }
         return ['code' => 0,'msg' => '获取成功','data' => $ad['data'],'count' => $ad['total'],'rel' => 1];
       }
       $cate = model('ad_class')->select();
       $this->assign('cate',$cate);
       return $this->fetch();
   }
    /**
     * @Notes 广告添加
     */
     public function add() {
        if(request()->isPost()) {
            $data = input('post.');
            $save = [
                'pid' => $data['pid'],
                'thumb' => $data['thumb'],
                'link' => $data['link'],
                'url' => $data['url'],
                'title' => $data['title'],
                'status' => $data['status'],
                'create_time' => time()
            ];
            $res = model('ad')->insert($save);
            if($res) {
                return ajaxArray(1,'保存成功');
            }
            return ajaxArray(0,'保存失败，请重新保存');
        }
        $positions = model('ad_class')->select();
        $this->assign('positions',$positions);
        return $this->fetch();
     }
    /**
     * @Notes 广告编辑
     */
    public function edit() {
        $id = input('id','');
        if(request()->isPost()) {
           $data = input('post.');
           $save = [
               'pid' => $data['pid'],
               'thumb' => $data['thumb'],
               'link' => $data['link'],
               'url' => $data['url'],
               'title' => $data['title'],
               'status' => $data['status']
           ];
           $res = model('ad')->where(['id' => $data['id']])->update($save);
           if($res) {
               return ajaxArray(1,'编辑成功');
           }
           return ajaxArray(0,'编辑失败，请重新尝试');
        }
        $ad = model('ad')->where(['id' => $id])->find();
        $positions = model('ad_class')->select();
        $this->assign('positions',$positions);
        $this->assign('ad',$ad);
        return $this->fetch();
    }
    /**
     * @Notes 广告状态编辑
     * @auth Mili-wang
     * @return array
     */
    public function state() {
        $id = input('post.id/d',0);
        $status = model('ad')->where('id',$id)->value('status');
        $status = $status ? 0 : 1;
        $result = model('ad')->where('id',$id)->setField('status', $status);
        if ($result !== false) {
            return ajaxArray(1,'编辑成功',$status);
        }
        return ajaxArray(0,'编辑失败');
    }

    /**
     * @Notes 广告删除
     */
    public function del() {
        $id = input('id/d');
        $res = model('ad')->where(['id' => $id])->delete();
        if($res) return ajaxArray(1,'删除成功');
        return ajaxArray(0,'删除失败');
     }

    /**
     * @Notes 广告定位
     */
    public function positions() {   
        if(request()->isPost()) {
            $pageSize = input('limit/d');
            $key = input('key','');
            $where = [];
            if($key) $where['name|position'] = ['like','%'.$key.'%'];
            $adclass = model('ad_class')->where($where)->paginate($pageSize)->toArray();
            return ['code' => 0, 'msg' => '获取成功','data' => $adclass['data'],'count' => $adclass['total'],'rel' => 1];
        }
        return $this->fetch();
    }
    /**
     * @Notes 广告定位添加
     */
    public function ad_add() {
      if(request()->isPost()) {
           $data = input('post.');
           $save = [
               'name' => $data['name'],
               'position' => $data['position']
           ];
           $res = model('ad_class')->insert($save);
           if($res) {
               return ajaxArray(1,'保存成功');
           }
           return ajaxArray(0,'保存失败，请重试');
      }
      return $this->fetch();
    }
    /**
     * @Notes 广告定位编辑
     */
    public function ad_edit() {
        $id = input('id/d');
        if(request()->isPost()) {
            $data = input('post.');
            $save = [
                'name' => $data['name'],
                'position' => $data['position']
            ];
            $res = model('ad_class')->where(['id' => $data['id']])->update($save);
            if($res) {
                return ajaxArray(1,'保存成功');
            }
            return ajaxArray(0,'保存失败，请重试');
        }
        $adclass = model('ad_class')->where(['id' => $id])->find();
        $this->assign('adclass',$adclass);
        return $this->fetch();
    }
    /**
     * @Notes 广告定位删除
     */
    public function ad_del() {
        $id = input('id/d');
        if(empty($id)) return ajaxArray(0,'数据错误');
        $canDel = model('ad')->where('pid',$id)->count();
        if($canDel > 0) return ajaxArray(0,'当前定位使用中不可删除');
        $res = model('ad_class')->where(['id' => $id])->delete();
        if($res) return ajaxArray(1,'删除成功');
        return ajaxArray(0,'删除失败');
    }

    /**
     * @Notes table 编辑更新
     */
    public function update() {
        if(request()->isPost()) {
            $data = input('post.');
            $res = model('ad')->where('id','eq', $data['id'])->update([$data['field'] => $data['value']]);
            if($res) {
                return ajaxArray(1,'修改成功');
            }
            return ajaxArray(0,'请重新编辑');
        }
    }
    /**
     * @Notes table 编辑更新
     */
    public function updatePosition() {
        if(request()->isPost()) {
            $data = input('post.');
            $res = model('ad_class')->where('id','eq', $data['id'])->update([$data['field'] => $data['value']]);
            if($res) {
                return ajaxArray(1,'修改成功');
            }
            return ajaxArray(0,'请重新编辑');
        }
    }
}